package com.blrs.controller;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.blrs.common.annotation.RepeatSubmit;
import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.SupplierInfo;
import com.blrs.domain.vo.SupplierBankCountVo;
import com.blrs.service.supplier.ISupplierInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;

import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-05-17
 */
@RestController
@RequestMapping("/supplier/info")
public class SupplierInfoController extends BaseController
{
    @Autowired
    private ISupplierInfoService supplierInfoService;

    /**
     * 查询【请填写功能名称】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SupplierInfo supplierInfo)
    {
        startPage();
        List<SupplierInfo> list = supplierInfoService.selectSupplierInfoList(supplierInfo);
        return getDataTable(list);
    }

    @GetMapping("/listType")
    public TableDataInfo listType(SupplierInfo supplierInfo)
    {
        startPage();
        List<SupplierInfo> list = supplierInfoService.selectCooperativeRelationshipList(supplierInfo);
        return getDataTable(list);
    }

    @GetMapping("/listToActivity")
    public TableDataInfo listToActivity(SupplierInfo supplierInfo)
    {
        startPage();
        List<SupplierInfo> list = supplierInfoService.selectSupplierListToActivity(supplierInfo);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SupplierInfo supplierInfo)
    {
        List<SupplierInfo> list = supplierInfoService.selectSupplierInfoList(supplierInfo);
        ExcelUtil<SupplierInfo> util = new ExcelUtil<SupplierInfo>(SupplierInfo.class);
        util.exportExcel(response, list, "供应商信息数据");
    }

    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, SupplierInfo supplierInfo)
    {
        List<SupplierInfo> list = supplierInfoService.selectCooperativeRelationshipList(supplierInfo);
        ExcelUtil<SupplierInfo> util = new ExcelUtil<SupplierInfo>(SupplierInfo.class);
        util.exportExcel(response, list, "平台关系信息数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{enterpriseId}")
    public AjaxResult getInfo(@PathVariable("enterpriseId") String enterpriseId)
    {
        return success(supplierInfoService.selectSupplierInfoByEnterpriseId(enterpriseId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult add(@RequestBody SupplierInfo supplierInfo)
    {
        return toAjax(supplierInfoService.insertSupplierInfo(supplierInfo));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit(interval = 1000,message = "请勿重复提交！")
    public AjaxResult edit(@RequestBody SupplierInfo supplierInfo)
    {
        return toAjax(supplierInfoService.updateSupplierInfo(supplierInfo));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enterpriseIds}")
    public AjaxResult remove(@PathVariable String[] enterpriseIds)
    {
        return toAjax(supplierInfoService.deleteSupplierInfoByEnterpriseIds(enterpriseIds));
    }

    @GetMapping(value = "/getMaxEnterpriseNo/{industryType}")
    public String getMaxEnterpriseNo(@PathVariable String industryType){
        String belongTo = SecurityUtils.getOrgId().toString();
        return supplierInfoService.getMaxEnterpriseNo(belongTo,industryType);
    }

    @GetMapping(value = "/getEnterpriseName/{enterpriseName}")
    public SupplierInfo getEnterpriseName(@PathVariable String enterpriseName){
        String belongTo = SecurityUtils.getOrgId().toString();
        return supplierInfoService.selectEnterpriseNameByName(enterpriseName,belongTo);
    }

    @PostMapping("/saveSupplierBank")
    public AjaxResult saveSupplierBank(@RequestBody SupplierBankCountVo supplierBankCountVo) {
        return toAjax(supplierInfoService.saveSupplierInfoList(supplierBankCountVo));
    }

    @PostMapping("/updateSupplierBank")
    public AjaxResult updateSupplierBank(@RequestBody SupplierBankCountVo supplierBankCountVo) {
        return toAjax(supplierInfoService.updateSupplierInfoList(supplierBankCountVo));
    }

    @GetMapping("/platformOrganization")
    public TableDataInfo platformOrganization(SysDept sysDept) {
        if (Objects.isNull(sysDept.getDeptId())){
            return getDataTable(Collections.emptyList());
        }
        List<Long> list=supplierInfoService.selectPlatformOrganization(sysDept.getDeptId());
        startPage();
        //根据id去查询企业信息
        List<SysDept> sysDepts = supplierInfoService.selectSupplierInfoListByDeptId(list,sysDept.getDeptName());
        return getDataTable(sysDepts);
    }

}
